package com.mzp.delivery.delivery.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;
import com.mzp.delivery.delivery.dao.DeliveryDao;
import com.mzp.delivery.delivery.pojo.Delivery;

/**
 * @Description user_delivery 服务层
 * @author admin
 * @date 2021-04-19 10:42:24
*/
@Service
public class DeliveryService {

	@Autowired
	private DeliveryDao deliveryDao;

	@Autowired
	private IdWorker idWorker;

	/**
	* 查询全部列表
	* @return
	*/
	public List<Delivery> findAll() {
		return deliveryDao.findAll();
	}

	/**
	* 条件查询+分页
	* @param whereMap
	* @param page
	* @param size
	* @return
	*/
	public Page<Delivery> findSearch(Map whereMap, int page, int size) {
		Specification<Delivery> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return deliveryDao.findAll(specification, pageRequest);
	}

	/**
 	* 条件查询
	* @param whereMap
	* @return
	*/
	public List<Delivery> findSearch(Map whereMap) {
		Specification<Delivery> specification = createSpecification(whereMap);
		return deliveryDao.findAll(specification);
	}

	/**
	* 根据ID查询实体
	* @param id
	* @return
	*/
	public Delivery findById(String id) {
		return deliveryDao.findById(id).get();
	}

	/**
	* 增加
	* @param delivery
	*/
	public void add(Delivery delivery) {
		delivery.setId( idWorker.nextId()+"" );
		deliveryDao.save(delivery);
	}

	/**
	* 修改
	* @param delivery
	*/
	public void update(Delivery delivery) {
		deliveryDao.save(delivery);
	}

	/**
	* 删除
	* @param id
	*/
	public void deleteById(String id) {
		deliveryDao.deleteById(id);
	}

	/**
	* 动态条件构建
	* @param searchMap
	* @return
	*/
	private Specification<Delivery> createSpecification(Map searchMap) {

		return new Specification<Delivery>() {

			@Override
			public Predicate toPredicate(Root<Delivery> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
				//userid
				if (searchMap.get("userid")!=null && !"".equals(searchMap.get("userid"))) {
					predicateList.add(cb.like(root.get("userid").as(String.class), "%"+(String)searchMap.get("userid")+"%"));
				}
				//username
				if (searchMap.get("username")!=null && !"".equals(searchMap.get("username"))) {
					predicateList.add(cb.like(root.get("username").as(String.class), "%"+(String)searchMap.get("username")+"%"));
				}
				//postal
				if (searchMap.get("postal")!=null && !"".equals(searchMap.get("postal"))) {
					predicateList.add(cb.like(root.get("postal").as(String.class), "%"+(String)searchMap.get("postal")+"%"));
				}
				//phone
				if (searchMap.get("phone")!=null && !"".equals(searchMap.get("phone"))) {
					predicateList.add(cb.like(root.get("phone").as(String.class), "%"+(String)searchMap.get("phone")+"%"));
				}
				//userdelivery
				if (searchMap.get("userdelivery")!=null && !"".equals(searchMap.get("userdelivery"))) {
					predicateList.add(cb.like(root.get("userdelivery").as(String.class), "%"+(String)searchMap.get("userdelivery")+"%"));
				}
				//detaildelivery
				if (searchMap.get("detaildelivery")!=null && !"".equals(searchMap.get("detaildelivery"))) {
					predicateList.add(cb.like(root.get("detaildelivery").as(String.class), "%"+(String)searchMap.get("detaildelivery")+"%"));
				}

				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
			}
		};
	}

}
